<?php
/**
 * Helper for phpunit log formating.
 *
 * @category PhpCilux
 * @package  PhpCilux
 */

/**
 * Helper for phpunit log formating.
 *
 * @package PhpCilux
 */
class Zend_View_Helper_PhpUnit extends Zend_View_Helper_Abstract
{

    /**
     * Formating the php unit log.
     *
     * @param string $content The log content which will be styled.
     * 
     * @return string
     */
    public function phpUnit($content)
    {
        $lines = explode("\n", $content);

        $content = '<h4>PHPUnit</h4>';

        $totalLines = count($lines);

        while (list($key, $line) = each($lines)) {
            $line = rtrim($line);

            switch(true) {
                case 0 == $key:
                case 1 == $key && '' == trim($line):
                case $key == ($totalLines - 1):
                    break;

                case stripos($line, '[2K') !== false:
                    $pos  = stripos($line, '[2K');
                    $line = substr($line, ($pos + 3));

                    if ('OK' === substr($line, 0, 2)) {
                        $content .= '<div class="success">' . $line . '</div>';
                    } elseif ('FAILURES' == substr($line, 0, 8)) {
                        $content .= '<div class="failed">' . $line . '</div>';
                    } else {
                        $content .= $line . "\n";
                    }
                    break;

                default:
                    $content .= $line . "\n";
                    break;
            }
        }

        return $content;
    }

}